@echo off

@rem Set build directory information
if not exist "%~1" call :output_error 1 "didn't get pl_appname!" & goto :ERR

set pl_appname=%~1
set pl_appdir=%~dp0%pl_appname%
set pl_outdir=%~dp0out\%pl_appname%
set pl_outfile=
set pl_error=0

if not exist "%pl_outdir%" mkdir "%pl_outdir%"

@rem Set the device you want to build for to 1
set PRE=1
set PIXI=0
set DEBUG=0

@rem List your source files here
@rem set SRC=

@rem List the default libraries needed
@rem set LIBS=-lSDL -lGLESv2

@rem Name your output executable
set OUTFILE=

call "%pl_appdir%\plugin\buildcfg.bat"

if "%SRC%"==""        call :output_error 1 "didn't get SRC!" & goto :ERR
if "%OUTFILE%"==""    call :output_error 1 "didn't get OUTFILE!" & goto :ERR

set pl_outfile=%OUTFILE%

if exist "%pl_outdir%\%pl_outfile%" del /f /q "%pl_outdir%\%pl_outfile%"

if %PRE% equ 0 if %PIXI% equ 0 call :output_error 1 "please specify the device's type, PRE or PIXI!" & goto :ERR

if %DEBUG% equ 1 (
   set DEVICEOPTS=-g
) else (
   set DEVICEOPTS=
)

if %PRE% equ 1 (
   set DEVICEOPTS=%DEVICEOPTS% -mcpu=cortex-a8 -mfpu=neon -mfloat-abi=softfp
)

if %PIXI% equ 1 (
   set DEVICEOPTS=%DEVICEOPTS% -mcpu=arm1136jf-s -mfpu=vfp -mfloat-abi=softfp
)

pushd "%pl_appdir%\plugin"
	arm-none-linux-gnueabi-gcc %DEVICEOPTS% -o "%pl_outdir%\%pl_outfile%" %SRC% "-I%PALMPDK%\include" "-I%PALMPDK%\include\SDL" "-I%pl_appdir%\plugin" "-L%PALMPDK%\device\lib" -Wl,--allow-shlib-undefined %LIBS% || (call :output_error 2 & goto :ERR)
popd

call :output_finish "generate %pl_outfile% in %pl_outdir%!"

:ERR
exit /b %pl_error%

:output_finish
	echo.
	if not "%~1"=="" echo ***Finish: %~1
	echo.
	set pl_error=0
	goto :eof

:output_error
	set pl_error=%1
	if not "%~2"=="" echo ***Error: %~2
	goto :eof